package com.tea.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tea.Model.GoodsSpec;
import com.tea.Model.dto.SpecStockUpdateDTO;
import com.tea.Model.dto.BatchSpecStockUpdateDTO;
import com.tea.mapper.GoodsSpecMapper;
import com.tea.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class GoodsSpecService extends ServiceImpl<GoodsSpecMapper, GoodsSpec> {
    
    /**
     * 更新单个规格库存
     * @param dto 库存更新信息
     * @return 更新结果
     */
    @Transactional
    public Result<Object> updateSpecStock(SpecStockUpdateDTO dto) {
        try {
            GoodsSpec spec = getById(dto.getSpecId());
            if (spec == null) {
                return Result.error("规格不存在");
            }

            Long currentStock = spec.getStockNum() != null ? spec.getStockNum() : 0L;
            Long newStock = currentStock;

            switch (dto.getOperation()) {
                case "increase":
                    newStock = currentStock + dto.getQuantity();
                    break;
                case "decrease":
                    newStock = currentStock - dto.getQuantity();
                    if (newStock < 0) {
                        return Result.error("库存不足，无法减少" + dto.getQuantity() + "件");
                    }
                    break;
                case "set":
                    if (dto.getQuantity() < 0) {
                        return Result.error("库存数量不能小于0");
                    }
                    newStock = dto.getQuantity();
                    break;
                default:
                    return Result.error("无效的操作类型，支持：increase, decrease, set");
            }

            UpdateWrapper<GoodsSpec> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", dto.getSpecId())
                        .set("stock_num", newStock);
            
            boolean success = update(updateWrapper);
            
            if (success) {
                return Result.success("库存更新成功，当前库存：" + newStock);
            } else {
                return Result.error("库存更新失败");
            }
            
        } catch (Exception e) {
            return Result.error("更新库存失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量更新规格库存
     * @param dto 批量库存更新信息
     * @return 更新结果
     */
    @Transactional
    public Result<Object> batchUpdateSpecStock(BatchSpecStockUpdateDTO dto) {
        try {
            for (SpecStockUpdateDTO item : dto.getItems()) {
                Result<Object> result = updateSpecStock(item);
                if (result.getCode() != 200) {
                    // 如果任何一个更新失败，返回失败信息
                    return result;
                }
            }
            return Result.success("批量库存更新成功");
        } catch (Exception e) {
            return Result.error("批量更新库存失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取规格库存信息
     * @param specId 规格ID
     * @return 规格信息
     */
    public Result<GoodsSpec> getSpecStock(Long specId) {
        GoodsSpec spec = getById(specId);
        if (spec == null) {
            return Result.error("规格不存在");
        }
        return Result.success(spec);
    }
}
